﻿
jQuery(function ()
{

    //框架
    var southHeight = $("body").height() - $("#lay-north").height();
    $("#lay-south").height(southHeight);
    $("#lay-south").kendoSplitter({
        panes:
            [
            { collapsible: false }
            ]
    });
    $("#lay-center").kendoSplitter({
        orientation: "vertical",
        panes:
            [
            { collapsible: false, resizable: false, size: "0px" },
            { collapsible: false }
            ]
    });

    //用户
    $("#popUser").html($("#popUserTemp").html());
    $("#popUserTemp").remove();
    $("body").click(function ()
    {
        $(".profile").hide();
    });
    $(".profile").click(function (event)
    {
        event.stopPropagation();
    });
    $(".user").click(function (event)
    {
        event.stopPropagation();
        $(".profile").toggle();
    });
});

function changePassword(url)
{
    activeTab('Common_User_ChangePassword', '修改密码', url);
    $(".profile").hide();
}

function personal(url)
{
    activeTab('Common_User_Personal', '个人资料', url);
    $(".profile").hide();
}

function activeTab(key, name, url, unclose)
{
    var keyNext = key + "_Next";
    var next = $("#iframes").find(bykey(keyNext));
    if (next.length > 0)
    {
        $("#tabstrip").find("a").removeClass("selected");
        $("#tabstrip").find(bykey(key)).addClass("selected");

        $("#iframes").find("iframe").hide();
        $("#iframes").find(bykey(keyNext)).show();
    }
    else
    {
        var exist = $("#tabstrip").find(bykey(key));
        if (exist.length > 0)
        {
            $("#tabstrip").find("a").removeClass("selected");
            $("#tabstrip").find(bykey(key)).addClass("selected");

            $("#iframes").find("iframe").hide();
            $("#iframes").find(bykey(key)).show();
        }
        else
        {
            if (unclose)
            {
                $("#tabstrip").append("<a class='unclose' key='" + key + "' onclick='activeTab(\"" + key + "\")'>" + name + "</a>");
            }
            else
            {
                $("#tabstrip").append("<a key='" + key + "' onclick='activeTab(\"" + key + "\")'>" + name + "<i onclick='removeTab(event,\"" + key + "\")'>×</i></a>");
            }

            $("#tabstrip").find("a").removeClass("selected");
            $("#tabstrip").find("a:last").addClass("selected");

            $("#iframes").find("iframe").hide();
            $("#iframes").append("<iframe id='" + key + "' name='" + key + "' key='" + key + "' src='" + url + "' frameborder='0' scrolling='no'></iframe>");
        }
    }
}

function removeTab(event, key)
{
    if (window.event)
    {
        event.cancelBubble = true;
    } else if (event)
    {
        event.stopPropagation();
    }

    $("#tabstrip").find(bykey(key)).remove();
    $("#iframes").find(bykeyAll(key)).remove();
    if ($.browser.msie)
    {
        CollectGarbage();
    }

    $("#tabstrip").find("a").removeClass("selected");
    $("#tabstrip").find("a:last").addClass("selected");

    $("#iframes").find("iframe:last").show();
    if ($("#iframes").find("iframe").length > 0)
    {
        var nextKey = $("#tabstrip").find("a:last").attr("key");
    }

    return false;
}

function navTab(url)
{
    var key = $("#iframes").find("iframe:visible").attr("key");
    var keyNext = key + "_Next";
    $("#iframes").find("iframe").hide();
    $("#iframes").append("<iframe id='" + keyNext + "' name='" + keyNext + "' key='" + keyNext + "' src='" + url + "' frameborder='0' scrolling='no'></iframe>");
}

function backTab(reload, data, message)
{
    if (message)
        alert(message);

    var key = $("#iframes").find("iframe:visible").attr("key");
    var keyPrev = key.substring(0, key.length - 5);

    $("#iframes").find("iframe").hide();
    $("#iframes").find(bykey(key)).remove();
    if ($.browser.msie)
    {
        CollectGarbage();
    }
    var prev = $("#iframes").find(bykey(keyPrev));
    if (prev.length > 0)
    {
        var index = 0;
        $("iframe").each(function (i)
        {
            if ($(this).attr("id") == keyPrev)
            {
                index = i;
            }
        });

        $("#iframes").find(bykey(keyPrev)).show();
        if (reload == "reloadTree")
        {
            window.frames[index].reloadTree(data);
        }
        else if (reload)
        {
            window.frames[index].reload(data);
        }
    }
    else
    {
        removeTab(key);
    }
}

function bykey(key)
{
    return "[key='" + key + "']";
}

function bykeyAll(key)
{
    return "[key*='" + key + "']";
}

var win = null;
var callback = null;
function winOpen(options, callbackFunc)
{
    options.close = function ()
    {
        win.destroy();
        win = null;
        $("#win").remove();
    };

    if (win) return;
    callback = callbackFunc;
    $("body").append("<div id='win'></div>");
    win = $("#win").kendoWindow(options).data("kendoWindow");
    win.center()
    win.open();
}


function winClose()
{
    if (!win) return;
    win.close();
    win.destroy();
    win = null;
    $("#win").remove();
}

function winSize(title, width, height)
{
    if (!win) return;

    win.setOptions(
    {
        title: title,
        width: width,
        height: height
    });
    win.center();
}